/*
Kiwi Editor is a framework for building WYSIWYG web editors.
Copyright (C) 2011  Roosevelt de Lima Sardinha

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Contact the author at roosevelt1@gmail.com (Roosevelt) 
*/

/**
 * Manages documents creation in the editor. A document is what you build in an edition area, containing all EOs as they were set by the user.
 * @author Roosevelt de Lima Sardinha
 * @constructor
 */
function editorDocumentManager(){
	
	/**
	 * Gets a document in the database from its id.
	 * @param {String} id The unique id of the document.
	 * @return The document that has the given id.
	 * @type {Object}
	 */
	this.get = function(id){
		// Returns a document
		var myRequest = new request();
        myRequest.sendSincRequest('GET', null, "documents/" + id, function(response){
            EOsStr = response;
        });
		var docJSON = JSON.parse(EOsStr);
		var doc = new editorDocument();
		
		doc.id = docJSON.id;
		doc.name = docJSON.name;
		doc.description = docJSON.description; 
		
		return doc;
	}
	
	/**
	 * Returns all documents in the database.
	 * @return An object with all documents and its EOs.
	 * @type {Object}
	 */
	this.all = function(){
		// Returns all documents
		var myRequest = new request();
        myRequest.sendSincRequest('GET', null, "documents", function(response){
            EOsStr = response;
        });
		var docsJSON = JSON.parse(EOsStr);
		
		return docsJSON;
	}
	
	/**
	 * Removes a document by the id
	 * @param {String} id The id of the document to remove.
	 */
	this.remove = function(id){
		// Removes a document by id
		var myRequest = new request();
		var params = [];
		params['_method'] = 'DELETE';
        myRequest.sendSincRequest('POST', params, "documents/" + id, function(response){});
	}
	
	/**
	 * Creates an empty document.
	 * @return The new document.
	 * @type {Object}
	 */
	this.createDocument = function(){
		//Creates  a new EditorDocument Object
		var doc = new editorDocument();
		return doc;
	}
	
	/**
	 * Creates a document using a given JSON.
	 * @param {Object} json The JSON that contains the properties of the document and its EOs.
	 */
	this.createDocumentByJSON = function(json){
		//Creates  a new EditorDocument object from a given JSON
		var doc = new editorDocument(json);		
		return doc;		
	}

}

